package com.yhcui.rabbit.rocetmq.demo;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.TimeUnit;

/**
 * 同步发送
 */
public class SynProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("producterGroup");
        producer.setNamesrvAddr("localhost:9876");
//        producer.setVipChannelEnabled(false);
        producer.setSendMsgTimeout(20000);

//        producer.setCreateTopicKey("topic");
        producer.start();
        for (int i = 0; i < 2; i++) {
            Message message = new Message("topic", "TagA", ("Hello " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(message);
            System.out.printf("%s%n", sendResult);
        }
        TimeUnit.SECONDS.sleep(3);

        for (int i = 3; i < 5; i++) {
            Message message = new Message("topic", "TagA", ("Hello " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(message);
            System.out.printf("%s%n", sendResult);
        }

        producer.shutdown();
    }
}
